import java.util.LinkedList;

/**
 * @author zhu
 * @date 2024-03-22
 */
public class Box {
    private LinkedList<Fruit> boxList = new LinkedList<>();
    private final static int BOX_MAX = 5;

    public synchronized void put(String name, Fruit fruit){
        if (boxList.size() == BOX_MAX){
            try {
                this.notify();
                this.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        boxList.add(fruit);
        System.out.println("篮子当前容量：" + boxList.size() + "，男孩子[" + name + "]摘水果:" + fruit);

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    public synchronized void take(String name){
        if (boxList.size() == 0){
            try {
                this.notify();
                this.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        Fruit fruit = this.boxList.removeLast();
        System.out.println("篮子当前容量：" + boxList.size() + "，女孩子[" + name + "]吃水果:" + fruit);

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
